﻿using System;
using System.Diagnostics;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.CompositeWeb.Utility;
using Microsoft.Practices.ObjectBuilder;

namespace CSC440TeamOne.MyHealth.Views
{
    /// <summary>
    /// Name - DailyEntryPresenter.cs
    /// Description - 
    /// Programmer - Matthew Mitchell
    /// Date coded - 11/27/2011
    /// Arguments - None
    /// Variables - None
    /// Files accessed by this code artifact - None
    /// Files changed by this code artifact - None
    /// I/O - None
    /// Error Handling - None
    /// Modifications -
    ///     1.0 - Initial coding
    /// Known Faults -
    ///     1.0 - None known
    /// </summary>
    public class DailyEntryPresenter : Presenter<IDailyEntryView>
    {
        private IMyHealthController _controller;

        /// <summary>
        /// Initializes a new instance of the <see cref="DailyEntryPresenter"/> class.
        /// </summary>
        /// <param name="controller">The controller.</param>
        public DailyEntryPresenter([CreateNew] IMyHealthController controller)
        {
            Guard.ArgumentNotNull(controller, "controller");

            _controller = controller;
        }

        /// <summary>
        /// Called when [view loaded].
        /// </summary>
        public override void OnViewLoaded()
        {

        }

        /// <summary>
        /// Called when [view initialized].
        /// </summary>
        public override void OnViewInitialized()
        {
            Trace.TraceInformation("start DailyEntryPresenter.OnViewInitialized");

            try
            {
                var model = _controller.LoadDailyEntryViewForCurrentUser();

                Debug.Assert(model != null);

                View.Show(model);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                throw;
            }

            Trace.TraceInformation("end DailyEntryPresenter.OnViewInitialized");
        }

        /// <summary>
        /// Called when [save].
        /// </summary>
        public void OnSave()
        {
            Trace.TraceInformation("start DailyEntryPresenter.OnSave");

            try
            {
                var model = _controller.LoadDailyEntryViewForCurrentUser();

                Debug.Assert(model != null);

                View.Update(model);

                _controller.SaveDailyEntryViewModel(model);

                View.OnSaveCompleted(model);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                throw;
            }

            Trace.TraceInformation("end DailyEntryPresenter.OnSave");
        }
    }
}




